import React, { Component } from 'react';
import axios from 'axios';

export default class Search extends Component {
    render() {
        return (
            <section className="jumbotron">
                <h3 className="jumbotron-heading">Search Github Users</h3>
                <div>
                    <input ref={el => this.iptEle = el} type="text" placeholder="enter the name you search" />&nbsp;
                    <button onClick={this.search}>Search</button>
                </div>
            </section>
        )
    }

    search = () => {
        const { getUservalue } = this.props;
        //获取用户输入的被搜索的内容
        const { value } = this.iptEle;
        //发生axios请求
        axios.get('https://api.github.com/search/users', {
            params: {
                q: value
            }
        })
            .then(res => {
                //console.log(res);//从这个github链接拿到数据
                const userInfo = res.data.items;
                 console.log(userInfo);//根据搜索框的内容拿到相关的数据
                //调用父组件的方法
                getUservalue(userInfo);
            })
            .catch(reason => {
                  console.log(reason);
            })
    }

}
